<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* "static methods controller" event handler
* @author J.Ducastel <jducastel@jducastel.fr>
* @version 
*/
class wakEventHandler_smController extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	*/
	function wakEventHandler_smController() {
		$this->__construct();
	}/**/
	
	/**
	* "real" constructor
	* @access
	*/
	function __construct() {
		parent::__construct();
		$this->deb=&wak::getservice('wakdebug');
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* executes an event
	* @param string $eventCall
	* @param wakParameters $params
	* @access public
	* @return mixed
	*/
	function run($eventCall, & $params) {
		$r=null;
		$deb=&wak::getservice('wakdebug');
		//echo "<p>running $eventCall</p>";
		
		// parsing call
		if (!$parsed=$this->_parseCall($eventCall)) {
			return $r;
		}
		$class=$parsed['class'];
		$method=$parsed['method'];
		// including class definition file if needed
		if (!class_exists($class)) {
			// gettingevent file node from eventCall
			$evtS=&wak::getservice('wakevents');
			$basename=$parsed['class'].'.smcontroller'; //echo $basename;
			if (!$file=$evtS->searchEventFile($basename)) {
				//trigger_error("wakEventHandler_script: could'nt find $eventCall file");
				trigger_error("wakEventHandler_script: could'nt find $eventCall file",E_USER_WARNING);
				return $r;
			}
			if (file_exists($file)) {
				include_once($file);
			} else if (file_exists('/'.$file)) {
				// dirty hack for web hosts using absolute paths
				include_once('/'.$file);
			}
			// checking class defined
			if (!class_exists($class)) {
				trigger_error("found file $file, class $class still undefined",E_USER_WARNING);
				return $r;
			}
		}
		// static method call
		if (!in_array($method,get_class_methods($class))) {
			trigger_error("event class $class hasn't method $method",E_USER_WARNING);
			return $r;
		}
		// getting call result
		//echo wakdebug::vardump($params);
		$r=call_user_func(array($class,$method),$params);
		// getting view if available
		$TS=&wak::getservice('waktemplatestorage');
		if ($tpl=&$TS->getById("$class.$method")) {
			$data=array(
				'result'=>$r
			);
			// rendering
			//$view=$tpl->render($data);
			//echo wakapi::vardump($data);
			$tpl->setData($data);
			//echo wakapi::vardump($tpl);
			//echo wakapi::vardump($tpl->render());
			//echo $tpl->render();
			$view=$tpl->render();
			return $view;
		}
		// echo $file;
		return $r;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	//var //
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* parse event call
	* @param string $call
	* @access private
	* @return array|false
	*/
	function _parseCall($call) {
		$segments=explode('.',$call);
		$class=isset($segments[1])?$segments[1]:null;
		if (!$class)
			return false;
		$method=isset($segments[2])?$segments[2]:null;
		return array('class'=>$class,'method'=>$method);
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
